/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment.v1;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardGridArrayAdapter;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.internal.CardThumbnail;
import it.gmariotti.cardslib.library.view.CardGridView;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ScrollView;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * List base example
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class GridBaseFragment extends MaterialV1Fragment {

    protected ScrollView mScrollView;

    @Override
    protected int getSubTitleHeaderResourceId() {
        return ResourceTable.String_header_title_subtitle_gbase;
    }

    @Override
    protected int getTitleHeaderResourceId() {
        return ResourceTable.String_header_title_group3;
    }

    @Override
    protected String getDocUrl() {
        return "";
    }

    @Override
    protected String getSourceUrl() {
        return "";
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_demo_fragment_grid_base);
        initCards();
    }


    private void initCards() {

        ArrayList<Card> cards = new ArrayList<Card>();
        for (int i = 0; i < 200; i++) {
            GdriveGridCard card = new GdriveGridCard(this);

            card.headerTitle = "Folder " + i;
            card.init();
            cards.add(card);
        }

        CardGridArrayAdapter mCardArrayAdapter = new CardGridArrayAdapter(this, cards);

        CardGridView gridView = (CardGridView) this.findComponentById(ResourceTable.Id_carddemo_grid_base);
        if (gridView != null) {
            gridView.setAdapter(mCardArrayAdapter);
        }
    }


    public class GdriveGridCard extends Card {

        protected String headerTitle;

        public GdriveGridCard(Context context) {
            super(context);
        }

        private void init() {
            CardHeader header = new CardHeader(getContext(), ResourceTable.Layout_carddemo_gdrive_header_inner);
            header.setTitle(headerTitle);
            header.setOtherButtonVisible(true);

            //Add a callback
            header.setOtherButtonClickListener((card, view) -> new ToastDialog(getContext()).setText("Click on Other Button").show());

            //Use this code to set your drawable
            header.setOtherButtonDrawable(ResourceTable.Graphic_card_menu_button_expand);

            addCardHeader(header);

            GdriveGridThumb thumbnail = new GdriveGridThumb(getContext());
            thumbnail.setDrawableResource(ResourceTable.Media_ic_action_folder_closed);
            addCardThumbnail(thumbnail);

            setClickedListener((card, view) -> new ToastDialog(getContext()).setText("Click Listener card=" + headerTitle).show());

            setSwipeable(true);


        }

        class GdriveGridThumb extends CardThumbnail {

            public GdriveGridThumb(Context context) {
                super(context);
            }

            @Override
            public void setupInnerViewElements(ComponentContainer parent, Component viewImage) {
                viewImage.getLayoutConfig().width = 32;
                viewImage.getLayoutConfig().height = 32;

            }
        }

    }

}
